package com.balabala.filter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.Iterator;
import java.util.Set;

/**
 * @Author 何涛
 * @Date: 2023/04/25/ 10:14
 * @description 不用拦截路径的白名单
 */
@Configuration
public class IgnoreWhiteListProperties {
    @Autowired
    public Admin admin;
    @Autowired
    public App app;
    @Configuration
    @ConfigurationProperties("security.admin")
    public class Admin
    {
        Set<String> whites;
        public Admin() {
        }

        public Set<String> getWhites() {
            return whites;
        }

        public void setWhites(Set<String> whites) {
            this.whites = whites;
        }
    }
    @Configuration
    @ConfigurationProperties("security.app")
    public class App
    {
        Set<String> whites;

        public App() {
        }

        public Set<String> getWhites() {
            return whites;
        }

        public void setWhites(Set<String> whites) {
            this.whites = whites;
        }
    }

    public Admin getAdmin() {
        return admin;
    }

    public void setAdmin(Admin admin) {
        this.admin = admin;
    }

    public App getApp() {
        return app;
    }

    public void setApp(App app) {
        this.app = app;
    }
    public boolean isIgnore(String uri)
    {
        if(admin==null||admin.whites==null) {
            return false;
        }
        if(app==null||app.whites==null){
            return false;
        }
        Iterator<String> adminIterator = admin.whites.iterator();
        while (adminIterator.hasNext()) {
            String adminWhite = adminIterator.next();
            if(uri.contains(adminWhite)) {
                return true;
            }
        }
        Iterator<String> appIterator = app.whites.iterator();
        while (appIterator.hasNext()) {
            String appWhite = appIterator.next();
            if(uri.contains(appWhite)) {
                return true;
            }
        }
        return false;
    }
}
